package com.supermap.localization.util;

import java.io.BufferedReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;

public class PropertiesHelper {

    public static Properties loadProperties(String path) throws Exception {
        Properties prop = new Properties();
        Path propFile = Paths.get(path);
        BufferedReader buffReader = Files.newBufferedReader(propFile, StandardCharsets.UTF_8);
        String line = null;
        while ((line = buffReader.readLine()) != null) {
            line = StringUtils.trim(line);
            if (line.startsWith("#") || line.equals("")) {// 忽略#开头的注释
                continue;
            }
            int index = line.indexOf('=');
            if (index <= 0) {
                throw new Exception("错误的配置文件格式!");
            }
            String key = StringUtils.trim(line.substring(0, index));
            String value = (index + 1 >= line.length()) ? "" : StringUtils.trim(line.substring(index + 1));
            prop.put(key, value);
        }

        return prop;
    }

}
